<template>
  <div class="welcome-card">
    <div class="welcome-title">
      <div class="welcome-title-left-container">
        <image v-if="isTikTok" :src="iconQuestion" class="img-in-tt"></image>
        <Icon v-else :src="iconQuestion" />
        <p
          v-if="title"
          class="card-title"
        >
          {{ title }}
        </p>
      </div>
      <div
        class="change-wrapper"
        @click="changeBranchList()"
      >
        <image v-if="isTikTok" :src="iconRefresh" class="img-in-tt"></image>
        <Icon v-else :src="iconRefresh" />
      </div>
    </div>
    <div class="welcome-items-container">
      <div
        v-for="(item, index) in showList"
        :key="index"
        class="welcome-item"
        @click="handleContentListItemClick(item)"
      >
        <div class="item-content">{{ item.content }}</div>
      </div>
      <div class="welcome-item welcome-item-hidden">
        {{ longestContent }}
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import vue from '../../../../../../adapter-vue';
import imRobotGuess from '../../../../../../assets/imRobotGuess.svg';
import refresh from '../../../../../../assets/refresh.svg';
import iconRight from '../../../../../../assets/iconRight.svg';
import Icon from './customer-icon.vue';
import iconQuestion from '../../../../../../assets/icon_question.png';
import iconRefresh from '../../../../../../assets/icon_refresh.png';
import { customerServicePayloadType } from '../../../../../../interface';
import { isTikTok } from "../../../../../../utils/env";

const { reactive, toRefs, computed } = vue;

interface Props {
  payload: customerServicePayloadType;
}

interface welcomeBranchItem {
  id: string;
  content: string;
  answer: string;
}

export default {
  components: {
    Icon,
  },
  props: {
    payload: {
      type: Object as () => customerServicePayloadType,
      default: () => ({ content: { title: '', items: [] }}),
    },
  },
  emits: ['sendMessage'],
  setup(props: Props, { emit }) {
    const data = reactive({
      // title
      title: props.payload?.content?.title || '',
      // all branch list
      list: props.payload?.content?.items || [],
      // current branch list
      showList: (props.payload?.content?.items || []).slice(0, 3),
      // current page number
      pageNumber: 1,
    });

    const handleContentListItemClick = (branch: welcomeBranchItem) => {
      emit('sendMessage', { text: branch.content });
    };

    const changeBranchList = () => {
      if (data.pageNumber * 3 >= data.list?.length) {
        data.pageNumber = 0;
      }
      data.showList = data.list?.slice(
        data.pageNumber * 3,
        data.pageNumber * 3 + 3,
      );
      data.pageNumber += 1;
    };

    const longestContent = computed(() => {
      if (!data.list) {
        return '';
      }
      let longestContent = '';
      data.list.forEach((item) => {
        if (item.content.length > longestContent.length) {
          longestContent = item.content;
        }
      });
      return longestContent;
    });

    return {
      ...toRefs(data),
      handleContentListItemClick,
      imRobotGuess,
      refresh,
      iconRight,
      changeBranchList,
      iconQuestion,
      iconRefresh,
      longestContent,
      isTikTok,
    };
  },
};
</script>

<style lang="scss">
.welcome-card {
  max-width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .welcome-title {
    display: flex;
    height: 20px;
    margin-bottom: 10px;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 10px;
  }

  .welcome-title-left-container {
    display: flex;
    align-items: center;
  }

  .card-title {
    display: inline-block;
    margin-left: 8px;
    font-size: 14px;
  }

  .change-wrapper {
    display: flex;
    cursor: pointer;
  }

  .welcome-items-container {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .welcome-item {
      width: 100%;
      padding: 6px 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      background-color: #ffffff;
      margin-bottom: 10px;
      border: 1px solid #adcfff;
      border-radius: 20px;
      font-weight: 500;
      color: #1c66e5;
      gap: 10px;
      .item-content {
        text-align: center;
        overflow-wrap: break-word;
        word-break: normal;
        width: 100%;
        padding: 0 10px;
      }
    }

    .welcome-item:hover {
      background: #f2f7ff;
    }

    .welcome-item:last-child {
      margin-bottom: 0;
    }

    .welcome-item-hidden {
      visibility: hidden;
      padding: 0;
      margin: 0;
      height: 0;
    }
  }
}

.img-in-tt {
  width: 16px;
  height: 16px;
}

</style>
